<template>
  <a-card :bordered="false">
    <div class="table-operator" style="text-align: right;">
      <a-button type="primary" class="btn_add" @click="$refs.addForm.add()" icon="plus">新增</a-button>
    </div>

    <s-table
      ref="table"
      size="default"
      :columns="columns"
      :data="loadData"
      :alert="true"
      :rowKey="(record) => record.id"
      id="table-wrapper"
    >

      <span slot="status" slot-scope="text">
        {{ text == 0? '禁用':'启用' }}
      </span>

      <span slot="gatewayTypeCode" slot-scope="text">
        {{ collectorTypeFliter(text) }}
      </span>

      <span slot="action" slot-scope="text, record">
        <a @click="$refs.editForm.edit(record)" class="btn_edit">编辑</a>
        <a-divider type="vertical"/>
        <a-popconfirm placement="topRight" class="btn_del" title="确认删除？" @confirm="() => del(record)">
          <a>删除</a>
        </a-popconfirm>
      </span>
    </s-table>

    <add-form ref="addForm" @ok="handleOk"/>
    <edit-form ref="editForm" @ok="handleOk"/>

  </a-card>
</template>

<script>
  import { STable } from '@/components'
  import { page, del } from '@/api/modular/main/bizcollectorconfigtemplate/bizCollectorConfigTemplateManage'
  import addForm from './addForm'
  import editForm from './editForm'
  // import { gatewayTypelist } from '@/api/modular/main/bizgatewaytype/bizGatewayTypeManage'
  import { sysDictTypeDropDown } from '@/api/modular/system/dictManage'

  export default {
    components: {
      STable,
      addForm,
      editForm
    },

    data () {
      return {
        advanced: false, // 高级搜索 展开/关闭
        queryParam: {}, // 查询参数
        columns: [ // 表头
          {
            title: '名称',
            dataIndex: 'configName'
          },
          {
            title: '编码',
            dataIndex: 'code'
          },
          {
            title: '值',
            dataIndex: 'value'
          },
          {
            title: '状态',
            dataIndex: 'status',
            scopedSlots: { customRender: 'status' }
          },
          {
            title: '备注',
            dataIndex: 'remark'
          },
          {
            title: '采集器类型',
            dataIndex: 'gatewayTypeCode',
            scopedSlots: { customRender: 'gatewayTypeCode' },
            width: '120px'
          },
          {
            title: '操作',
            dataIndex: 'action',
            align: 'center',

            width: '150px',
            scopedSlots: { customRender: 'action' }
          }
        ],
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          return page(Object.assign(parameter, this.queryParam)).then((res) => {
            return res.data
          })
        },
        selectedRowKeys: [],
        selectedRows: [],
        defaultExpandedKeys: [],
        replaceFields: {
          key: 'id'
        },
        gatewayTypelist: [],
        collectorTypelist: []
      }
    },
    created () {
      this.sysDictTypeDropDown()
    },
    methods: {
      del (record) {
        del(record).then((res) => {
          if (res.success) {
            this.$message.success('删除成功')
            this.$refs.table.refresh()
          } else {
            this.$message.error('删除失败：' + res.message)
          }
        }).catch((err) => {
          this.$message.error('删除错误：' + err.message)
        })
      },
      sysDictTypeDropDown () {
        sysDictTypeDropDown({ code: 'collector_type' }).then((res) => {
          this.collectorTypelist = res.data
        })
      },
      collectorTypeFliter (text) {
        const values = this.collectorTypelist.filter(item => item.code === text)
        if (values.length > 0) {
          return values[0].value
        }
      },
      toggleAdvanced () {
        this.advanced = !this.advanced
      },
      handleOk () {
        this.$refs.table.refresh()
      },
      onSelectChange (selectedRowKeys, selectedRows) {
        this.selectedRowKeys = selectedRowKeys
        this.selectedRows = selectedRows
      }
    }
  }
</script>

<style lang="less">
.table-operator {
  margin-bottom: 18px;
}
button {
  margin-right: 8px;
}
</style>
